package entity
{
	import Sync.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	
	public class EntityManager
	{
		private static var s_ins:EntityManager = null;
		private var m_nextHandle:int;
		private var m_handleMapEntity:Array;	
		private var m_main:Sprite = null;
		private var m_btn:SimpleButton = null;
		
		private var m_playerHandles:Array;

		public function EntityManager()
		{
			m_nextHandle = 0;
		}
		
		public static function getInstance():EntityManager
		{
			if (s_ins == null)
			{
				s_ins = new EntityManager();
			}
			
			return s_ins;
		}
		
		public function initialize(_main:Sprite):void
		{
			m_handleMapEntity = new Array();
			m_playerHandles = new Array();
			
			m_main = _main;
		}
		
		public function release():void
		{
			
		}
		
		private function getNextHandle():int
		{
			m_nextHandle++;
			
			return m_nextHandle;
		}
		
		public function createStartButton():void
		{
			if (m_btn == null)
			{
				m_btn = new SimpleButton();
				
				m_btn.upState = createShape(0x006600);
				m_btn.overState = createShape(0x009900);
				m_btn.downState = createShape(0x006600);
				m_btn.hitTestState = createShape(0x006600);
				
				m_btn.addEventListener(MouseEvent.CLICK, onStart);

				m_main.addChild(m_btn);
			}
		}
		
		public function destroyStartButton():void
		{
			if (m_btn != null)
			{
				m_btn.removeEventListener(MouseEvent.CLICK, onStart);
				m_main.removeChild(m_btn);
				m_btn = null;
			}
		}
		
		public function onStart(e:MouseEvent):void
		{
			SyncSystem.getInstance().beginSendMsg(ConstValue.MSG_ID_START_SYNC);
			SyncSystem.getInstance().endSendMsg();
		}
		
		private function createShape(color:uint):Shape 
		{
			var circle:Shape=new Shape();
			circle.graphics.beginFill(color);
			circle.graphics.drawRect(0,0,50,20);
			circle.graphics.endFill();
			return circle;
		}
		
		public function createEntity(name:String):int
		{
			if (name == "player")
			{
				var handle:int = getNextHandle();
				var _entity:EntityPlayer = new EntityPlayer(handle);
				
				var obj:HandleMapEntity = new HandleMapEntity();
				
				obj.m_handle = handle;
				obj.m_entity = _entity;
				
				m_main.addChild(_entity);
				
				m_handleMapEntity.push(obj);
				
				m_playerHandles.push(handle);
				
				return handle;
			}
			
			// ...
			
			return -1;
		}
		
		public function destoryEntity(handle:int):void
		{
			for (var count:int = 0; count < m_handleMapEntity.length; ++count)
			{
				var obj:HandleMapEntity = m_handleMapEntity[count];
				
				if (obj.m_handle == handle)
				{
					m_handleMapEntity.splice(count, 1);
					
					break;
				}
			}
		}
		
		public function getEntity(handle:int):Entity
		{
			for (var count:int = 0; count < m_handleMapEntity.length; ++count)
			{
				var obj:HandleMapEntity = m_handleMapEntity[count];
				
				if (obj.m_handle == handle)
				{
					return obj.m_entity;
				}
			}
			
			return null;
		}
		
		public function onMessage(sender:int, receiver:int, instruct:int, stream:Stream):void
		{
			for (var count:int = 0; count < m_handleMapEntity.length; ++count)
			{
				var obj:HandleMapEntity = m_handleMapEntity[count];
				
				if (obj.m_handle == receiver)
				{
					obj.m_entity.onMessage(sender, instruct, stream);
					
					break;
				}
			}
		}
		
		public function logicStep(frameTime:int):void
		{
			for (var count:int = 0; count < m_handleMapEntity.length; ++count)
			{
				var obj:HandleMapEntity = m_handleMapEntity[count];
				
				obj.m_entity.logicStep(frameTime);
			}
		}	
		
		public function getPlayerHandles():Array
		{
			return m_playerHandles;
		}
		
	}
	

	
}